/**
 * 
 */
package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.DefaultCaret;

import tp.pr5.Constants;
import tp.pr5.Direction;
import tp.pr5.NavigationObserver;
import tp.pr5.PlaceInfo;


/**
 * @author VICTOR
 *
 */
public class NavigationPanel extends JPanel implements NavigationObserver{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final static int COLUMS = 11;
	private final static int ROWS = 11;
	private final static int initXPosition = 5;
	private final static int initYPosition = 5;
	private PlaceCell[][] placeCellPanelArray;
	private int xPosition;
	private int yPosition;
	private JLabel orientation;
	private JPanel logPanel;
	private JTextArea log;
	private JPanel placeCellPanel;
	private ImageIcon northImage;
	private ImageIcon eastImage;
	private ImageIcon southImage;
	private ImageIcon westImage;
	private InfoPanel infoPanel;
	
	/**
	 * Constructor
	 * @param MainWindow mainWindow
	 */
	public NavigationPanel(MainWindow mainWindow){
		this.infoPanel = mainWindow.getInfoPanel();
		this.northImage = new ImageIcon(this.getClass().getResource("/tp/pr5/gui/images/walleNorth.png"));
		this.eastImage = new ImageIcon(this.getClass().getResource("/tp/pr5/gui/images/walleEast.png"));
		this.southImage = new ImageIcon(this.getClass().getResource("/tp/pr5/gui/images/walleSouth.png"));
		this.westImage = new ImageIcon(this.getClass().getResource("/tp/pr5/gui/images/walleWest.png"));
		this.xPosition = initXPosition;
		this.yPosition = initYPosition;
		build();
	}
	
	private void build()
	{
		this.setLayout(new BorderLayout());

		this.log = new JTextArea();
		this.log.setEditable(false);
		this.log.setColumns(80);
		this.log.setRows(8);
		DefaultCaret caret = (DefaultCaret)this.log.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		
		
		JScrollPane scroll = new JScrollPane(this.log);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		
		this.orientation = new JLabel();
		this.orientation.setIcon(this.northImage);
		
		this.logPanel = new JPanel();
		this.logPanel.setLayout(new BorderLayout());
		this.logPanel.setBorder(BorderFactory.createTitledBorder("Log"));
		this.logPanel.add(scroll, BorderLayout.CENTER);
		
		
		this.add(this.logPanel,BorderLayout.SOUTH);
		this.add(this.orientation, BorderLayout.WEST);
		

		this.placeCellPanel = new JPanel();
		
		this.placeCellPanel.setLayout(new GridLayout(ROWS,COLUMS));
		this.placeCellPanel.setBorder(BorderFactory.createTitledBorder("City Map"));
		
		this.placeCellPanelArray = new PlaceCell[ROWS][COLUMS]; 
		
		for (int i = 0; i<11; i++)
		{
			for (int j = 0; j<11; j++)
			{
				this.placeCellPanelArray[j][i] = new PlaceCell(this);
				this.placeCellPanel.add(this.placeCellPanelArray[j][i]);
			}
		}
		
		this.add(placeCellPanel, BorderLayout.CENTER);
	}
	
	/**
	 * set direction to robot current heading
	 * @param Direction direction
	 */
	private void setDirection(Direction direction)
	{
		switch(direction)
		{
			case NORTH:
			{
				this.orientation.setIcon(this.northImage);
				break;
			}
			case EAST:
			{
				this.orientation.setIcon(this.eastImage);
				break;
			}
			case SOUTH:
			{
				this.orientation.setIcon(this.southImage);
				break;
			}
			case WEST:
			{		
				this.orientation.setIcon(this.westImage);
				break;
			}
			default:
				break;
		}
	}
	
	/**
	 * Move the robot in GUI
	 * @param Place nextPlace
	 */
	private void move(Direction dir, PlaceInfo nextPlace)
	{
		this.placeCellPanelArray[xPosition][yPosition].left();
        switch(dir)
        {
                case NORTH:
                {
                        this.yPosition--;
                        break;
                }
                case EAST:
                {
                        this.xPosition++;
                        break;
                }
                case SOUTH:
                {
                        this.yPosition++;
                        break;
                }
                case WEST:
                {               
                        this.xPosition--;
                        break;
                }
                default:
                        break;
        }

        this.placeCellPanelArray[xPosition][yPosition].arriveAt(nextPlace);
 //       logInfo(nextPlace.getDescription());
	}
	
	private void logInfo(String info)
	{
		this.log.append(info);
	}

	@Override
	public void headingChanged(Direction newHeading) {
		setDirection(newHeading);
		this.infoPanel.headingChanged(newHeading);
	}

	@Override
	public void initNavigationModule(PlaceInfo initialPlace, Direction heading) {
        this.placeCellPanelArray[initXPosition][initXPosition].arriveAt(initialPlace);
		setDirection(heading);
		this.infoPanel.initNavigationModule(initialPlace, heading);
	}

	@Override
	public void robotArrivesAtPlace(Direction heading, PlaceInfo place) {
		move(heading, place);
		this.logInfo(place.toString() + Constants.LINE_SEPARATOR + Constants.LINE_SEPARATOR);
		this.infoPanel.placeHasChanged(place);
		this.infoPanel.robotArrivesAtPlace(heading, place);
	}

	@Override
	public void placeScanned(PlaceInfo placeDescription) {
		this.logInfo(placeDescription.toString() + Constants.LINE_SEPARATOR + Constants.LINE_SEPARATOR);
		this.infoPanel.placeScanned(placeDescription);
	}

	@Override
	public void placeHasChanged(PlaceInfo placeDescription) {}
	
	public PlaceCell getCurrentCell()
	{
		return this.placeCellPanelArray[xPosition][yPosition];
	}
	
	public void showCurrentPlace(String description)
	{
		this.logInfo(description);
	}
	
	
}
